import http from './http'
import { serverInfo } from '../models/serverInfo'
import { getUnikey, getQR, checkQR } from '@/models/loginAndRegister'
import { UserAccount, } from '@/models/user'
import { GetAllDetailMusics, GetDetailMusicList, GetSubcount, MusicPlayList, PlayList } from '@/models/musciLists'
import { GetMusic } from '@/models/music'
import { SearchAll, SearchSong } from '@/models/search'
import { Recommend } from '@/models/recommend'
import { AllVideo, Mv, MvData } from '@/models/mv'
import { Vedio, VedioUrl } from '@/models/vedio'
import { Lrc } from '@/models/lyric'

const timestamp = new Date().getTime()
// console.log(timer)

export async function login(phone: string, password: string) {
    const data = await http.post<{
        data: serverInfo
    }>(`/login/cellphone?phone=${phone}&password=${password}`, {
        phone: phone,
        password: password
    })
    return data
}

/* 搜索 */
export async function search(keywords: string) {
    const encodedKeywords = encodeURIComponent(keywords);
    const data = await http.post<{
        code: number, result: SearchAll, songs: SearchSong[]
    }>(`/cloudsearch?keywords=${encodedKeywords}`, {
        keywords: keywords
    })
    return data
}

/* 二维码登录 */
/* 获得二维码key */
export async function getKey() {
    return await http.get<{ data: getUnikey }>(`/login/qr/key`)
}
/*  二维码生成接口 */
export async function createKey(key: string) {chuansadasdsadoasdopasdas
    return await http.post<{
        code: number, data: getQR
    }>(`/login/qr/create`, {
        key: key,
        timestamp: new Date().getTime(),
        qrimg: true
    })
    // return data
}
/* 检测二维码状态 */
export async function checkKey(key: string) {
    return await http.get<{
        code: number,
        message: string,
        cookie: string
    }>(`/login/qr/check`, {
        key: key,
        timestamp: new Date().getTime(),
    })
}

/* 用户相关 */
/* 获得用户详情 */
export async function userDetail(id: number) {
    return await http.post<{ data: any }>(`/user/detail`, {
        uid: id
    })
}
/* 获得账号信息 */
export async function getAccount() {
    return await http.get<{
        code: number,
        profile: UserAccount
    }>('/user/account')
}
/* 获取用户信息，歌单，收藏，mv，dj，数量 */
export async function getSubcount() {
    return await http.get<{
        artistCount: number,
        code: number,
        createDjRadioCount: number,
        createdPlaylistCount: number,//创建的歌单
        djRadioCount: number,
        mvCount: number,
        newProgramCount: number,
        programCount: number,
        subPlaylistCount: number,//收藏的歌单
    }>('/user/subcount')
}

/* 歌单 */
/* 获取用户歌单 */
export async function getUerPlayList(uid: number) {
    return await http.get<{
        code: number, playlist: PlayList[]
    }>('/user/playlist', {
        uid: uid
    })
}
/* 获取歌单详情 */
export async function getMusicListsDetail(id: number) {
    return await http.get<{
        code: number, playlist: MusicPlayList
    }>('/playlist/detail', {
        id: id
    })
}
/* 获取歌单所有歌曲 */
export async function getAllMusicLists(id: number, len: number) {
    return await http.get<{
        code: number, songs: GetAllDetailMusics[]
    }>(`/playlist/track/all?id=${id}&limit=${len}&offset=0`)
}

/* 歌曲 */
export async function getMusicUrl(id: number) {
    return await http.get<{
        code: number
        data: GetMusic[]
    }>('/song/url', {
        id: id
    })
}

/* 推荐 */
/* 获取每日推荐歌单 */
export async function getResource() {
    return await http.get<{ code: number, recommend: Recommend[] }>('/recommend/resource')
}
/* 获取每日推荐歌曲 */
export async function getSongs() {
    return await http.get<{ data: any }>('/recommend/songs')
}

/* mv */
/* 获得mv地址 */
export async function getMvUrl(id: number | string) {
    const { data } = await http.get<{ data: Mv }>('/mv/url', {
        id: id
    })
    return data
}
/* 获得mv详情 */
export async function getMvDetail(id: number | string) {
    const { data } = await http.get<{ data: MvData }>('/mv/detail', {
        mvid: id
    })
    return data
}
/* 相关视频 */
export async function getAllvideo(id: number | string) {
    const { data } = await http.get<{ data: AllVideo }>('/related/allvideo', {
        id: id
    })
    return data
}

/* vedio */
/* 新的获取地址 */
export async function getCloudMusic(id: number | string) {
    const { data } = await http.get<{ data: Vedio }>('/video/detail', {
        id: id
    })
    return data
}
/* 获取vedio地址 */
export async function getVedioUrl(id: string) {
    const { urls } = await http.get<{ urls: VedioUrl[] }>('/video/url', {
        id: id
    })
    return urls
}

/* 退出登录 */
export async function logout() {
    const { code } = await http.get<{ code: number }>('/logout')
    return code
}

/* 歌词 */
/* 获取歌词 */
export async function getAllLyric(id: number) {
    const { lrc } = await http.get<{ lrc: Lrc }>('/lyric', {
        id: id
    })
    return lrc
}
/* 获取逐词歌词 */
export async function getNewLyric(id: number) {
    return await http.get<{ data: any }>('/lyric/new', {
        id: id
    })
}

/* 获取歌词评论 */
export async function getCommon(id: number) {
    return await http.get<{ data: any }>('/comment/music', {
        id: id
    })
}